
package com.hulk.ratel.manage.commoncache;


import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.SecurityRteException;
import com.hulk.ratel.common.security.RSAKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import lombok.NonNull;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author cmt
 * @E-mail:29572320@qq.com
 * @version Create on: 2017年4月29日 下午8:46:26 Class description
 */
@Slf4j
@Service
public class SecurityPartnerCache {

	//相对路径
	public static final String KEYPATH_TYPE_RELATIVE = "RELATIVE";
	//绝对路径
	public static final String KEYPATH_TYPE_ABSOLUTE = "ABSOLUTE";
	//数据库存储
	public static final String KEYPATH_TYPE_DATABASE = "DATABASE";

	@Autowired
	public Environment env;

	@Cached(key = "#partnerNo + #publicKeyPath + 'SecurityPartnerCache_publicKeyPath'", cacheType = CacheType.LOCAL ,expire =600)
	public PublicKey findPublicKey(@NonNull String partnerNo, @NonNull String publicKeyPath, @NonNull String suffix, @NonNull String  keyPathType) {

		log.info("partnerNo:{},findPublicKey:{},publicKeyPath:{}",
				new Object[] { "partnerNo", "findPublicKey", publicKeyPath});
		// 此处根据环境进行切换目录方法
//		String[] activeProfiles = env.getActiveProfiles();
//		String profile = Arrays.toString(activeProfiles);
		if (KEYPATH_TYPE_RELATIVE.equals(keyPathType)) {
			PublicKey publicKey = RSAKey.getRSAPublicKeyByRelativeFileSuffix(publicKeyPath, suffix);
			return publicKey;
		} else if(KEYPATH_TYPE_ABSOLUTE.equals(keyPathType)) {
			PublicKey publicKey = RSAKey.getRSAPublicKeyByAbsoluteFileSuffix(publicKeyPath, suffix);
			return publicKey;
		} else if(KEYPATH_TYPE_DATABASE.equals(keyPathType)){
			PublicKey publicKey = 	RSAKey.getRSAPublicKeyByStrSuffix(publicKeyPath,suffix);
			return publicKey;
		} else {
			throw new SecurityRteException("",BussErrCode.E_100010.getErrCode(),BussErrCode.E_100010.getErrDesc());
		}

	}

	@Cached(key = "#partnerNo + #privateKeyPath + 'SecurityPartnerCache_findPrivateKey'", cacheType = CacheType.LOCAL ,localLimit = 2048, localExpire =600)
	public PrivateKey findPrivateKey(@NonNull String partnerNo,@NonNull String privateKeyPath, @NonNull String suffix,@NonNull String keyPathType) {

		log.info("partnerNo:{},findPrivateKey:{},privateKeyPath:{}",
				new Object[] { "partnerNo", "findPrivateKey", privateKeyPath, });
		// 此处根据环境进行切换目录方法
//		String[] activeProfiles = env.getActiveProfiles();
//		String profile = Arrays.toString(activeProfiles);
		if (KEYPATH_TYPE_RELATIVE.equals(keyPathType)) {
			PrivateKey privateKey = RSAKey.getRSAPrivateKeyByRelativePathFileSuffix(privateKeyPath, suffix);
			return privateKey;
		} else if(KEYPATH_TYPE_ABSOLUTE.equals(keyPathType)) {
			PrivateKey privateKey = RSAKey.getRSAPrivateKeyByAbsoluteFileSuffix(privateKeyPath, suffix);
			return privateKey;
		} else if(KEYPATH_TYPE_DATABASE.equals(keyPathType)){
			PrivateKey privateKey = 	RSAKey.getRSAPrivateKeyByStrSuffix(privateKeyPath,suffix);
			return privateKey;
		} else {
			throw new SecurityRteException("",BussErrCode.E_100020.getErrCode(),BussErrCode.E_100020.getErrDesc());
		}

	}

}
